Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow type casting from strings to numerics #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

akscram
Copy link
Contributor

@akscram akscram commented Mar 20, 2018

Number(strict=False).load("123") == 123

    Number(strict=False).load("123") == 123
@maximkulkin
Copy link
Owner

maximkulkin commented Mar 21, 2018

I was thinking about this in a slightly different way: there is a use case for parsing non-string types from strings. Normally, when used with JSON, JSON parser parses strings into numbers so lollipop numeric types get numbers. However, there is a case when no pre-parser available (e.g. parsing query string parameters) so it would be nice to have a mode/types that can tolerate that.

Although this is not the same as allowing either representation (as is here). What I propose is to introduce parameter like as_string=True or stringified=True which will change behavor of non-string types (e.g. numbers or booleans) to parse & generate strings instead.

I was thinking of some kind of modifier e.g. Stringified(Integer()), but parsing of particular type is very type-specific and can not be done in a generic way outside of the type itself.

@akscram
Copy link
Contributor Author

akscram commented Mar 22, 2018

I am wondering, as this parsing is type-specific would it make sense to implement parsing in existing classes? For example by introducing an additional class, e.g. StringMixin, with a method, e.g. as_string, and then explicitly derive existing types with specific implementation, e.g. Integer(StringMixin, Type). So, this new modifier class could check that a particular type supports stringification just by checking on isinstance(inner_type, StringMixin) at Stringified(Integer()). I am worried about one thing - existing types should be changed and it is orthogonal to what you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants